// index.js

// 获取应用实例
const app = getApp()

const api = require('../../utils/util');

Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},
    data: {
        imagesUrl: app.globalData.imagesUrl,
        siteUrl: app.globalData.siteUrl,
        //商机
        announcement: '',
        //判断登录状态
        isLogin:false,
        showLogin: false,
        // 搜素
        // 搜索框
        page_show:false,
        navHeight: '',
        menuButtonInfo: {},
        searchMarginTop: 0, // 搜索框上边距
        searchWidth: 0, // 搜索框宽度
        searchHeight: 0 ,// 搜索框高度
        value: '',
        // banner数据
        banCurrentSwiper: 0,
        banAutoplay:true,
        images: [],
        banCircular:true,
        // 分类导航数据
        typeNavCurrentSwiper: 0,
        typeNavAutoplay:false,
        typeNavData: [
        ],
        // 列表数据
        list1: [
          
        ],
        list2: [
          
        ],
        list1Id: 1,
        // 切换置顶
        tabsList: [
            {
                title: '热门产品/服务'
            },
            {
                title: '热门企业'
            }
        ],
        tabsCurrent: 0,
        stickyScroll: false,
        enterprise: [],
         // 商机轮播
         noticeAutoplay: true,
         noticeCurrentSwiper: 0,
         noticeCircular: true,
         noticeLs: [
             {
                 title: "上海市绿色环保需要宣传海报制作"
             },
             {
                 title: "上海市绿色环保需要宣传海报制作2"
             }
         ],
         page:1,
         page1:1
    },
    // banner
    banSwiperChange: function (e) {
      if(e.detail.source == 'touch') {
        this.setData({
            banCurrentSwiper: e.detail.current
        });
      }

    },
    // 分类导航
    typeSwiperChange: function (e) {
        this.setData({
            typeNavCurrentSwiper: e.detail.current
        });
    },
    
    onShow() {
      
        var that = this;
        
        if(app.globalData.userinfo){
          that.setData({
            isLogin:true
          })
        }


        var systeminfo=wx.getSystemInfoSync()
        //console.log(systeminfo.windowHeight)
        this.setData({
            movehight:systeminfo.windowHeight,
            movehight2:systeminfo.windowHeight-100
        })
     
        this.setData({
            menuButtonInfo: wx.getMenuButtonBoundingClientRect()
        })
        console.log(this.data.menuButtonInfo)
        const { top, width, height, right } = this.data.menuButtonInfo
        wx.getSystemInfo({
            success: (res) => {
                const { statusBarHeight } = res
                const margin = top - statusBarHeight
                this.setData({
                    navHeight: (height + statusBarHeight + (margin * 2)),
                    searchMarginTop: statusBarHeight + margin, // 状态栏 + 胶囊按钮边距
                    searchHeight: height,  // 与胶囊按钮同高
                    searchWidth: right - width -20// 胶囊按钮右边坐标 - 胶囊按钮宽度 = 按钮左边可使用宽度
                })
            }
        }),
         //幻灯片
         this.banner(that),
         //瀑布流
         //this.update(),
         //商机
         this.announcement(that),
         //分类
         this.category(that),
         //获取企业列表
         this.get_enterprise(that, 1),

        this.product(that,1)

        api.getSupply1_3('').then(function(res){
           that.setData({
            noticeLs: res.data
           })
        })
    },
    // 刷新新瀑布流
    update(isFlag) {
      // 重置瀑布流组件
      if(isFlag){
        //this.selectComponent('#waterfall').reset()
      }
    
      let list1 = this.getMockData()
      this.setData({ list1 },()=> {
        console.log(this.data.list1)
        wx.hideLoading();
          wx.stopPullDownRefresh()
      })
      //this.selectComponent('#waterfall').reset()
     
  },
    //幻灯片
    banner(that){
       api.get_banner().then(function(res){
         console.log(res.data)
         let banner = [];
         for(let i = 0; i< res.data.length; i++){
            banner.push({
               pic : app.globalData.siteUrl + res.data[i].pic,
               company_id: res.data[i].company_id,
               type:res.data[i].type,
               link:res.data[i].link,
            })
         }
         that.setData({
           images: banner
         })
        
       })
    },
    enterprise(that){
      api.enterprise_list().then(function(res){
        that.setData({
          enterprise: res.data
        })
      })
    },
    //获取商机
    announcement(that){
       api.get_announcement().then(function(res){
        that.setData({
          announcement: res.data.title
        })
       })
    },
    //获取分类
    category(that){
    
      api.get_category().then(function(res){
        let a = [];
      
        for(let i = 0; i< res.data.length; i++){
          let b = []
           for(let j = 0; j< res.data[i].list.length; j++){
             b.push({
               title: res.data[i].list[j].title,
               ico: app.globalData.siteUrl + res.data[i].list[j].ico
             })
           }
           a.push({
            list: b
          })
        }
         console.log(a)
    
         that.setData({
          typeNavData: a
         })
      })
    },
    // 获取瀑布流数据
    // 获取瀑布流数据
    getMockData() {
      let { list1Id } = this.data
      let list1 = []
      for (let i = 0; i < this.data.list2.length; i++) {
          list1.push({
              list1Id,
              imgUrl: app.globalData.siteUrl + this.data.list2[i].url,
              title: this.data.list2[i].title+'',
              price: this.data.list2[i].price+'',
              companyLogoUrl: app.globalData.siteUrl + this.data.list2[i].companyLogoUrl,
              companyTitle: this.data.list2[i].companyTitle+'',
              tage: this.data.list2[i].tag+'',
              is_discuss: this.data.list2[i].is_discuss+'',
              id: this.data.list2[i].id,
              sub_title: this.data.list2[i].sub_title
          })

          this.data.list1Id = ++list1Id
      }
      return list1
  },

//获取产品
product(that, page){
  let param = {
    str: '',
    category: ''
  }
  api.product_listp(param, page).then(function(res){
    
    if (res.data.length>0){
      let list2 = []
      for(let i = 0; i< res.data.length; i++){
        list2.push({
          url: res.data[i].url,
          title: res.data[i].title+'',
          price: res.data[i].price+'',
          companyLogoUrl: res.data[i].logo,
          companyTitle: res.data[i].companyName+'',
          tag: res.data[i].category+'',
          is_discuss: res.data[i].is_discuss+'',
          id: res.data[i].id,
          sub_title:res.data[i].sub_title
        })
      }
      that.setData({
        list2: page == 1 ? list2 : that.data.list2.concat(list2),//合并数据
        page: page + 1
     },()=>{
       if(page > 1 ){
        that.update(false)
       }else{
         that.update(true)
       }
      
     })
      
    }else{
      wx.hideLoading();
        wx.showToast({
          title: '没有更多数据了',
          ico: 'error'
        })
    }
  
  })
},

get_enterprise(that, page){
  let param = {
    str: '',
    page_num: 6,
    category: ''
  }
  api.enterprise_list_page(param, page).then(function(res){
    if (res.data.length>0){
      that.setData({
        enterprise: page == 1 ? res.data : that.data.enterprise.concat(res.data),//合并数据
        page1: page + 1
      })
    }else{
        wx.showToast({
          title: '没有更多数据了',
          ico: 'error'
        })
    }
    wx.hideLoading();
  })
},

    // 切换置顶
    onStickyScroll(e) {
        let stickyScroll = false
        if(e.detail.isFixed) {
            stickyScroll = !stickyScroll
        }
        this.setData({stickyScroll})
    },
    // 切换
    onTabsClick(e) {
        let tabsCurrent = e.target.dataset.index
        this.setData({tabsCurrent})
        console.log(tabsCurrent)
    },
    toLogin: function(){
       wx.redirectTo({
         url: '/pages/login/login',
       })
    },
    showLogin(){
      this.setData({showLogin: true})
    },
    editHandle(e) {
      console.log(e);
    },
    more: function(){
      if(this.data.tabsCurrent == 0){
        wx.navigateTo({
          url: '/pages/index/product/product?category=',
        })
      }else{
        wx.navigateTo({
          url: '/pages/index/recommend/recommend',
        })
      }
    },
    onPullDownRefresh:function(){
      wx.showLoading();
      this.onRefresh();
    
    },
    onRefresh:function(){
        let that = this
        //导航条加载动画
        //wx.showNavigationBarLoading();
        wx.showLoading();
        setTimeout(function () {
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          that.setData({
            page: 1,
            list1Id: 1,
            page1:1
      
          })
          that.product(that, 1)
          that.get_enterprise(that,1)
        }, 2000);
      },
    onReachBottom:function(){
      if(this.data.tabsCurrent == 0){
        wx.showLoading()
        // 加载更多，滑动到底部执行此方法
        let page = this.data.page ;
        let that = this
        this.product(that, page)
        
    }
    if(this.data.tabsCurrent == 1){
      wx.showLoading()
      // 加载更多，滑动到底部执行此方法
      let page1 = this.data.page1
      let that = this
      this.get_enterprise(that, page1)
  }
  }
})
